<?php
/**
 * 项目分配
 * User: Administrator
 * Date: 2020/4/9
 * Time: 14:54
 */

namespace app\api\model;

use think\Db;
use think\Log;
use think\Model;

class ProjectAssign extends Model
{

    protected $name = 'project_assign';

    /**
     * 获取项目列表
     */
    public static function dataList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['uid'])) {
            $where['pa.uid'] = $params['uid'];
        }

        $list = db('project_assign')->alias('pa')->join('project p', 'p.id = pa.project_id', 'inner')->where($where)->field('p.*')->group('pa.project_id')->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                // 周期
                $cycleDay = (int)((strtotime($listVal['enddate']) - strtotime($listVal['startdate'])) / (24 * 3600));
                $cycleDay < 0 && $cycleDay = 0;
                // 剩余天数
                $remainingDay = (int)((strtotime($listVal['enddate']) - time()) / (24 * 3600));
                $remainingDay < 0 && $remainingDay = 0;
                $data = [
                    'pname' => $listVal['pname'],
                    'pcontent' => $listVal['pcontent'],
                    'cycle_days' => $cycleDay,
                    'team_numbers' => self::projectAssignNums($listVal['id']),
                    'remaining_day' => $remainingDay,
                    'is_end' => (int)$listVal['is_end']
                ];
                $listData[] = $data;
                unset($data);
            }
        }
        return $listData;
    }

    /**
     * 获取项目分配人数
     */
    public static function projectAssignNums($projectId)
    {
        return self::where(['project_id' => $projectId, 'uid' => ['neq', 0]])->count();
    }

    public static function findInfoByCondition($condition = [])
    {
        return self::where($condition)->find();
    }

    public static function findListByCondition($condition = [])
    {
        return self::where($condition)->select();
    }

    public static function getStatusName($status)
    {
        $str = '';
        switch ($status) {
            case '0':
                $str = '未开始';
                break;
            case '1':
                $str = '工作中';
                break;
            case '2':
                $str = '已完成';
                break;
            case '3':
                $str = '已退出';
                break;
        }
        return $str;
    }

    public static function addRecord($addData)
    {
        $data = $addData;
        $data['createtime'] = time();
        $data['updatetime'] = time();
        return self::create($data);
    }

    /**
     * 增加待审核时长
     */
    public static function incUnchckWorkHour($contidion = [], $hour)
    {
        if ($contidion && $hour > 0) {
            return self::where($contidion)->setInc('uncheck_work_hour', $hour);
        }
    }

    /**
     * 更新数据
     */
    public static function saveData($contidion = [], $data)
    {
        if ($contidion && $data) {
            $data['updatetime'] = time();
            return self::where($contidion)->update($data);
        }
    }

    public static function teams($params = [])
    {
        $where = [
            'pa.uid' => ['neq', 0]
        ];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['project_id'])) {
            $where['pa.project_id'] = $params['project_id'];
        }

        $list = db('project_assign')->alias('pa')->where($where)->field('pa.*')->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                //获取用户信息
                $user = User::findInfoByCondition(['id' => $listVal['uid']]);

                //获取岗位
                $category = Category::findInfoByCondition(['id' => $listVal['category_id']]);

                $data = [
                    'hua_name' => $user['hua_name'],
                    'mobile' => $user['mobile'],
                    'wechat' => $user['wechat'],
                    'category_name' => $category ? $category['name'] : '',
                    'headimg' => $user['avatar']
                ];
                $listData[] = $data;
                unset($data);
                unset($user);
                unset($category);
            }
        }
        return $listData;
    }

    public static function formatHours($hours)
    {
        $hours = $hours ? (sprintf('%.2f', ($hours / 100))) : 0;
        return $hours;
    }

}